package com.cip.master.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.apache.struts2.ServletActionContext;

import com.cip.admin.model.User;
import com.cip.common.util.SecurityUtil;
import com.cip.master.model.Album;
import com.cip.master.model.AlbumPicture;
import com.cip.master.service.AlbumService;
import com.cip.webapp.action.BaseAction;

public class AlbumAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private Album album;	
	private List<Album> albums;
	private AlbumService albumService;
	
	private List<File> files=new ArrayList<File>();
	private List<String> filesFileName = new ArrayList<String>();
	/*private String fileContentType;
	private String fileFileName;*/

	public String showAlbumList() throws Exception {
		setAlbum(new Album());
		
		Locale locale = getRequest().getLocale();
		String albumString = albumService.getAlbumGridList(locale);
		getRequest().setAttribute("albumGridString", albumString);
		
//		setAlbums(albumService.getAllAlbum());
		return SUCCESS;
	}
	
	public String showUserAlbums() throws Exception {
		setAlbums(albumService.getAllAlbum());
		return SUCCESS;
	}
	
	public String getAlbumById() {
		setAlbum(albumService.getAlbum(Integer.valueOf(getRequest().getParameter("id"))));
		return SUCCESS;
	}
	
	public String saveOrUpdateAlbum() throws Exception{
		User user = SecurityUtil.getThreadLocalUser().get();
		album.setTenantId(user.getTenantId());
		albumService.saveOrUpdate(album);
		  // the directory to upload to
        String uploadDir = ServletActionContext.getServletContext().getRealPath("/resources")
                + "/" + user.getTenantId()+ "/" + getRequest().getRemoteUser() + "/";
        
        String uploadDirRelativePath="/resources" + "/" + user.getTenantId()+"/"+ getRequest().getRemoteUser() + "/";
       Set<AlbumPicture> albumPictures=new HashSet<AlbumPicture>();
       int i=0;
        for (File file: files) {
        	AlbumPicture albumPicture=new AlbumPicture();    
        	String fileName=filesFileName.get(i).replaceAll(" ", "_");
        	albumPicture.setName(uploadDirRelativePath+fileName);
        	albumPicture.setAlbum(album);
        	createFile(uploadDir, file,fileName);
        	albumPictures.add(albumPicture);
	        i++;
	    }
        album.setAlbumPictures(albumPictures);
        albumService.saveOrUpdate(album);
        saveMessage(getText("album.added"));
		return SUCCESS;
	}
	
	public void createFile(String path,File file,String fileName) throws IOException{
		
		  // write the file to the file specified
        File dirPath = new File(path);

        if (!dirPath.exists()) {
            dirPath.mkdirs();
        }

        //retrieve the file data
        InputStream stream = new FileInputStream(file);

        //write the file to the file specified
        OutputStream bos = new FileOutputStream(path + fileName);
        int bytesRead;
        byte[] buffer = new byte[8192];

        while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
            bos.write(buffer, 0, bytesRead);
        }

        bos.close();
        stream.close();
		
	}
	
	
	public String showEditAlbum() {
		setAlbum(albumService.getAlbum(album.getId()));
		return SUCCESS;
	}
	
	public String showUserAlbumList() throws Exception{	
		setAlbums(albumService.getAllAlbum());
		return SUCCESS;
	}
	
	public Album getAlbum() {
		return album;
	}
	public void setAlbum(Album album) {
		this.album = album;
	}
	public AlbumService getAlbumService() {
		return albumService;
	}
	public void setAlbumService(AlbumService albumService) {
		this.albumService = albumService;
	}
	public List<Album> getAlbums() {
		return albums;
	}
	public void setAlbums(List<Album> albums) {
		this.albums = albums;
	}

	/*public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}*/

	public void setFiles(List<File> files) {
		this.files = files;
	}

	public List<File> getFiles() {
		return files;
	}

	public void setFilesFileName(List<String> filesFileName) {
		this.filesFileName = filesFileName;
	}

	public List<String> getFilesFileName() {
		return filesFileName;
	}
}
